package com.baidu.mpks.utils;

import com.baidu.mpks.common.HttpClientTemplate;
import com.baidu.mpks.util.JacksonUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Component
public class CommonUtils {
    private static final Logger log = LoggerFactory.getLogger(CommonUtils.class);
    static HttpClientTemplate template;

    @Autowired
    private CommonUtils(HttpClientTemplate template) {
        CommonUtils.template = template;
    }

    public static String timestampToDateTimeString(int timestamp, String format) {
        if (format == null) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        DateTimeFormatter ftf = DateTimeFormatter.ofPattern(format);
        return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochSecond((long)timestamp), ZoneId.systemDefault()));
    }

    public static long dateTimeStringToTimestamp(String datetime, String format) {
        if (StringUtils.isNotBlank(datetime)) {
            try {
                DateTimeFormatter ftf = DateTimeFormatter.ofPattern(format);
                LocalDateTime parse = LocalDateTime.parse(datetime, ftf);
                return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
            } catch (Exception var4) {
                log.error("date string is invalid: %s", String.valueOf(datetime));
                return -1L;
            }
        } else {
            return -1L;
        }
    }

    public static JsonNode httpGet(String url, Map<String, String> getParam) throws JsonProcessingException {
        log.info("httpGet url -------> {}", url);
        log.info("httpGet getParam ------> {}", getParam);

        try {
            String response = template.executeGet(url, getParam);
            JsonNode responseData = JacksonUtils.toJsonNode(response);
            log.debug("httpGet response -------> {}", responseData.get("data"));
            return responseData;
        } catch (IOException var4) {
            log.error(var4.getMessage());
            return null;
        }
    }

    public static JsonNode httpPost(String url, Object oriPostData) throws JsonProcessingException {
        log.info(url);
        String postData = (new ObjectMapper()).writeValueAsString(oriPostData);
        log.info("httpPost url -------> {}", url);
        log.info("httpPost postData ------> {}", postData);

        try {
            String response = template.executePostData(url, (Map)null, postData);
            JsonNode responseData = JacksonUtils.toJsonNode(response);
            log.info("httpPost response -------> {}", responseData.asText());
            return responseData;
        } catch (IOException var5) {
            log.error(var5.getMessage());
            return null;
        }
    }

    public static void copyFieldsIfExists(Map<String, Object> from, Map<String, Object> to, List<String> fieldList) {
        Iterator var3 = fieldList.iterator();

        while(var3.hasNext()) {
            String fieldName = (String)var3.next();
            if (from.containsKey(fieldName)) {
                to.put(fieldName, from.get(fieldName));
            }
        }

    }

    public static void copyFieldIfExists(Map<String, Object> from, Map<String, Object> to, String fromKey, String toKey) {
        if (from.containsKey(fromKey)) {
            to.put(toKey, from.get(fromKey));
        }

    }
}